package com.huifer.wgx.service.impl;

import com.huifer.wgx.geoverify.LineVerify;
import com.huifer.wgx.geoverify.PointVerify;
import com.huifer.wgx.model.DetectionLineEntity;
import com.huifer.wgx.model.DetectionPointEntity;
import com.huifer.wgx.model.InteriorLineEntity;
import com.huifer.wgx.model.InteriorPointEntity;
import com.huifer.wgx.model.PersonnelEntity;
import com.huifer.wgx.model.VerifyEntity;
import com.huifer.wgx.repository.DetectionLineEntityRepository;
import com.huifer.wgx.repository.InteriorLineRepository;
import com.huifer.wgx.repository.PersonnelRepository;
import com.huifer.wgx.repository.VerifyRepository;
import com.huifer.wgx.service.DetectionLineService;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

/**
 * <p>Title : DetectionLineServiceImpl </p>
 * <p>Description : </p>
 *
 * @author huifer
 * @date 2019-03-21
 */
@Service
public class DetectionLineServiceImpl implements DetectionLineService {

    @Autowired
    private DetectionLineEntityRepository detectionLineEntityRepository;
    @Autowired
    private VerifyRepository verifyRepository;
    @Autowired
    private PersonnelRepository personnelRepository;

    @Autowired
    private InteriorLineRepository interiorLineRepository;

    /**
     * 创建保存的数据
     *
     * @param verifyId 校验id
     * @param workId 工程id
     * @param personnelId 校验人员id
     * @return 校验结果列表
     */
    @Override
    public List<DetectionLineEntity> createList(Long verifyId, Long workId, Long personnelId) {
        Specification<InteriorLineEntity> ps = (Specification<InteriorLineEntity>)
                (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder
                        .equal(root.get("workId"), workId);
        List<InteriorLineEntity> all = interiorLineRepository.findAll(ps);
        List<DetectionLineEntity> ds = LineVerify.onlyPoint(all, verifyId, personnelId);
        return ds;
    }

    /**
     * 根据校验内容查询
     *
     * @param verifyName 校验名称
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验结果列表
     */
    @Override
    public Page<DetectionLineEntity> findByVerifyName(String verifyName, Integer pageNum,
            Integer pageSize) {
        Specification<VerifyEntity> specv = (Specification<VerifyEntity>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder
                .equal(root.get("verifyName"), verifyName);
        VerifyEntity verifyEntity = verifyRepository.findOne(specv).get();
        Specification<DetectionLineEntity> spec = (Specification<DetectionLineEntity>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder
                .equal(root.get("verifyId"), verifyEntity.getVerifyId());
        return getDetectionEntities(pageNum, pageSize, spec);
    }

    /**
     * 根据校验id查询
     *
     * @param verifyId 校验id
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验结果列表
     */
    @Override
    public Page<DetectionLineEntity> findByVerifyId(Long verifyId, Integer pageNum,
            Integer pageSize) {
        Specification<DetectionLineEntity> spec = (Specification<DetectionLineEntity>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder
                .equal(root.get("verifyId"), verifyId);
        return getDetectionEntities(pageNum, pageSize, spec);
    }

    /**
     * 根据校验人员名称查询
     *
     * @param personnelName 发起校验的人员名称
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验结果列表
     */
    @Override
    public Page<DetectionLineEntity> findByPersonnelName(String personnelName, Integer pageNum,
            Integer pageSize) {
        Specification<PersonnelEntity> specv = (Specification<PersonnelEntity>) (
                root, criteriaQuery, criteriaBuilder) -> criteriaBuilder
                .equal(root.get("personnelName"), personnelName);
        PersonnelEntity personnelEntity = personnelRepository.findOne(specv).get();
        Specification<DetectionLineEntity> spec = (Specification<DetectionLineEntity>)
                (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder
                        .equal(root.get("personnelId"), personnelEntity.getPersonnelId());
        return getDetectionEntities(pageNum, pageSize, spec);
    }

    /**
     * 根据校验人员id查询
     *
     * @param personnelId 校验人员id
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验结果列表
     */
    @Override
    public Page<DetectionLineEntity> findByPersonnelId(Long personnelId, Integer pageNum,
            Integer pageSize) {
        Specification<DetectionLineEntity> spec = (Specification<DetectionLineEntity>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder
                .equal(root.get("personnelId"), personnelId);
        return getDetectionEntities(pageNum, pageSize, spec);
    }

    /**
     * 保存校验结果列表
     *
     * @param detectionEntities 校验结果列表
     * @return 校验结果列表
     */
    @Override
    public List<DetectionLineEntity> saveDetectionList(
            List<DetectionLineEntity> detectionEntities) {
        try {
            return detectionLineEntityRepository.saveAll(detectionEntities);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 查询所有
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 校验结果列表
     */
    @Override
    public Page<DetectionLineEntity> findAll(Integer pageNum, Integer pageSize) {
        return getDetectionEntities(pageNum, pageSize, null);
    }


    /**
     * 获取全部校验结果
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @param spec 条件
     * @return 校验结果列表
     */
    private Page<DetectionLineEntity> getDetectionEntities(Integer pageNum, Integer pageSize,
            Specification<DetectionLineEntity> spec) {
        Sort sort = new Sort(Direction.DESC, "detectionId");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        return detectionLineEntityRepository.findAll(spec, pageable);
    }

}
